package ssm.blog.controller.onstage;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import ssm.blog.entity.User;
import ssm.blog.service.UserService;
import ssm.blog.util.MD5Util;
import ssm.blog.util.RandomValidateCode;
import ssm.blog.util.ResponseUtil;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: lyon
 * @description: 登陆控制层
 * @author: Lyon
 * @create: 2018-06-05 11:42
 **/
@Controller
@RequestMapping("/onstage")
public class LoginController {
    @Resource
    UserService userService;
    @RequestMapping("adminLogin.html")
    String login(HttpServletResponse response){
        return "interface/onstage/adminLogin";
    }
    @RequestMapping("adminLogin.do")
    String userLogin(String username, String password, HttpServletResponse response)throws Exception {
        JSONObject result = new JSONObject();
        //获取登陆实体
        Subject subject = SecurityUtils.getSubject();
        //获取加密后的密码
        String passWord = MD5Util.md5(password, "lyon");
        //获取用户登陆密码token
        UsernamePasswordToken token = new UsernamePasswordToken(username, passWord);
        try {
            subject.login(token);
            result.put("success", true);
            ResponseUtil.write(response, result);
            return null;
        } catch (AuthenticationException e) {
            result.put("success", false);
            result.put("msg", "账号名或密码错误!");
            ResponseUtil.write(response, result);
            return null;
        }
    }
    //暂时先将用户注册登陆关闭 等后面的配套设施 完成了 在一起打开
    //现在就当 一个个人博客用.
//    @RequestMapping("userLogin.html")
//    String userLogin()
//    {
//        return "interface/onstage/userLogin";
//    }
//    @RequestMapping("userSignUp.html")
//    String userSignUp()
//    {
//        return "interface/onstage/signUp";
//    }
    @RequestMapping("/getCode")
    void getCode(HttpServletRequest request, HttpServletResponse response){
        //设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");
        //设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            //输出验证码图片方法
            randomValidateCode.getRandcode(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/checkCode")
    String checkCode(String code, HttpSession session,HttpServletResponse response) throws Exception {
        //从session中获取随机数
        String random = (String) session.getAttribute("RANDOMVALIDATECODEKEY");
        JSONObject result=new JSONObject();
        if(random.equals(code)){
            result.put("success",true);
        }else{
            result.put("success",false);
        }
        ResponseUtil.write(response,result);
        return null;
    }
//    @RequestMapping("/checkUserName")
//    String checkUserName(String username,HttpServletResponse response) throws Exception {
//        int count=userService.countByUsername(username);
//        JSONObject result=new JSONObject();
//        if(count==0){
//            result.put("success",true);
//        }else{
//            result.put("success",false);
//        }
//        ResponseUtil.write(response,result);
//        return null;
//    }
//    @RequestMapping("/addUser")
//    String addUser(User user,HttpServletResponse response) throws Exception {
//        int resultRows=userService.addUser(user);
//        JSONObject result=new JSONObject();
//        if(resultRows>0){
//            result.put("success",true);
//        }else{
//            result.put("success",false);
//        }
//        ResponseUtil.write(response,result);
//        return null;
//    }
}
